package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.TCloudRole;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;

/**
 * 后台公用控制类
 * @author 徐震霖
 * @since 2018-11-13 20:18
 */
@Controller
@RequestMapping(value="/sysunifiedmanagement")
public class SysUnifiedManagementController {
	
	/**
	 * 获得某个学校的全部教师角色(添加教师时)
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 获得某个学校的全部教师角色(添加教师时)
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "获得某个学校的全部教师角色(添加教师时)", 
			operationClassPath = "controller.SysUnifiedManagementController.getTeacherRoleListBySchool(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "教师管理/教师信息添加")
	@RequestMapping(value="/getteacherrolelistbyschool")
	public String getTeacherRoleListBySchool(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression ex = new Expression();
				
		try {
			
			String schoolid = req.getParameter("schoolid");
			
			if(schoolid!=null && !schoolid.equals("")){
				ex.andEqu("schoolid", schoolid, String.class);
				ex.andEqu("parentid", Constant.TEACHER, Integer.class);
				ex.andEqu("isedit", 1, Boolean.class);
				
				String strWhere = ex.toString();
				
				List<TCloudRole> roleList = DAOFactory.getCloudRoleDAO().getRoleListByStrWhere(strWhere);
				if (roleList != null && roleList.size() > 0) {
					result.put("code", 0);
					result.put("msg", "查询成功");
					result.put("count", roleList.size());							
					JSONArray jsonArray = JSONArray.fromObject(roleList);
					result.element("data", jsonArray);
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "无查询结果");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "无查询结果");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "无查询结果");
		}
				
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 获得时间阶段处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 获得时间阶段处理类
	@RequestMapping(value="/getperiod")
	public String getPeriod(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		util.toolkit.Period period = new util.toolkit.Period();
		List<model.Period> periodList = new ArrayList<model.Period>();
		try {
			periodList = period.getPeriod(Constant.STAR_TIME_STRING);
			
			result.put("code", ResponseJSON.FLAG_SUCC);
			result.put("msg", "查询成功");
			result.put("count", periodList.size());
			JSONArray jsonArray = JSONArray.fromObject(periodList);
			result.element("data", jsonArray);
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "获取失败");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;

	}
	
}
